<template>
  <div class="app-container">
    <!-- 搜索和筛选区域 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" class="search-form">
      <el-form-item label="商品名称">
        <el-input v-model="queryParams.productName" placeholder="请输入商品名称" clearable />
      </el-form-item>
      <el-form-item label="非遗类别">
        <el-select v-model="queryParams.categoryId" placeholder="请选择类别" clearable>
          <el-option
            v-for="item in categoryOptions"
            :key="item.categoryId"
            :label="item.categoryName"
            :value="item.categoryId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="价格区间">
        <el-input-number v-model="queryParams.minPrice" :min="0" placeholder="最低价" />
        <span class="mx-2">-</span>
        <el-input-number v-model="queryParams.maxPrice" :min="0" placeholder="最高价" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 商品展示区域 -->
    <el-row :gutter="20" class="goods-list">
      <el-col :span="6" v-for="item in goodsList" :key="item.productId">
        <el-card :body-style="{ padding: '0px' }" class="goods-card" @click="handleViewDetail(item)">
          <img :src="item.image" class="goods-image">
          <div class="goods-info">
            <h3 class="goods-name">{{ item.productName }}</h3>
            <div class="goods-price">¥ {{ item.price }}</div>
            <div class="goods-desc">{{ item.description }}</div>
            <div class="goods-footer">
              <el-button type="primary" size="small" @click.stop="handleAddToCart(item)">加入购物车</el-button>
              <el-button type="danger" size="small" @click.stop="handleBuyNow(item)">立即购买</el-button>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="Goods">
import { getCurrentInstance, ref, reactive, toRefs, onMounted } from 'vue';
import { useRouter } from 'vue-router';

const { proxy } = getCurrentInstance();
const router = useRouter();

const goodsList = ref([]);
const categoryOptions = ref([]);
const loading = ref(true);
const total = ref(0);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 12,
    productName: undefined,
    categoryId: undefined,
    minPrice: undefined,
    maxPrice: undefined
  }
});

const { queryParams } = toRefs(data);

/** 查询商品列表 */
function getList() {
  loading.value = true;
  // TODO: 调用后端API获取商品列表
  loading.value = false;
}

/** 查询类别列表 */
function getCategories() {
  // TODO: 调用后端API获取类别列表
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryForm");
  handleQuery();
}

/** 查看商品详情 */
function handleViewDetail(item) {
  router.push(`/mall/detail/${item.productId}`);
}

/** 加入购物车 */
function handleAddToCart(item) {
  // TODO: 调用后端API添加到购物车
  proxy.$modal.msgSuccess("已加入购物车");
}

/** 立即购买 */
function handleBuyNow(item) {
  // TODO: 跳转到订单确认页面
  router.push({
    path: '/mall/order/confirm',
    query: {
      productId: item.productId,
      quantity: 1
    }
  });
}

onMounted(() => {
  getCategories();
  getList();
});
</script>

<style scoped>
.search-form {
  margin-bottom: 20px;
}
.goods-list {
  margin-bottom: 20px;
}
.goods-card {
  margin-bottom: 20px;
  cursor: pointer;
  transition: all 0.3s;
}
.goods-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
}
.goods-image {
  width: 100%;
  height: 200px;
  object-fit: cover;
}
.goods-info {
  padding: 14px;
}
.goods-name {
  font-size: 16px;
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.goods-price {
  color: #f56c6c;
  font-size: 20px;
  font-weight: bold;
  margin: 10px 0;
}
.goods-desc {
  font-size: 14px;
  color: #666;
  margin-bottom: 10px;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}
.goods-footer {
  display: flex;
  justify-content: space-between;
}
.mx-2 {
  margin: 0 10px;
}
</style> 